import { Service } from 'egg';
import { SetSystemControlValue } from '../util/form';
import {
  sortGroup,
  getGroupList,
  createGroup,
  renameGroup,
  removeGroup,
  sortForm,
  getFormList,
  createForm,
  renameForm,
  removeForm,
  saveForm,
  getFormData,
  createFormBiz,
  getFormBiz,
  removeFormBiz,
  saveFormBiz,
} from '../sql/form';

export default class form extends Service {
  async sortGroup(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const promiseArr: any = [];
    params.list.forEach((item) => {
      const { sql, param } = ctx.helper.formatSql(sortGroup, item);
      ctx.logger.info('sortGroup:', sql, param);
      promiseArr.push(
        new Promise(async (resolve) => {
          const res = await app.mysql.query(sql, param);
          resolve(res);
        }),
      );
    });
    await Promise.all(promiseArr);
    return null;
  }

  async getGroupList(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getGroupList, params);
    ctx.logger.info('getGroupList:', sql, params);
    const res = await app.mysql.query(sql, param);
    return res;
  }

  async createGroup(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(createGroup, params);
    ctx.logger.info('createGroup:', sql, params);
    await app.mysql.query(sql, param);
    return {
      formGroup: params.formGroup,
      groupName: params.groupName,
    };
  }

  async renameGroup(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(renameGroup, params);
    ctx.logger.info('renameGroup:', sql, params);
    await app.mysql.query(sql, param);
    return null;
  }

  async removeGroup(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(removeGroup, params);
    ctx.logger.info('removeGroup:', sql, params);
    await app.mysql.query(sql, param);
    return null;
  }

  async sortForm(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const promiseArr: any = [];
    params.list.forEach((item) => {
      const { sql, param } = ctx.helper.formatSql(sortForm, item);
      ctx.logger.info('sortForm:', sql, param);
      promiseArr.push(
        new Promise(async (resolve) => {
          const res = await app.mysql.query(sql, param);
          resolve(res);
        }),
      );
    });
    await Promise.all(promiseArr);
    return null;
  }

  async getFormList(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getFormList, params);
    ctx.logger.info('getFormList:', sql, params);
    let res: any = await app.mysql.query(sql, param);
    res = res.map((it: any) => {
      const item = ctx.helper.toJsonObj(it) || {};
      return item;
    });
    return res;
  }

  async createForm(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const formatParams = ctx.helper.toJsonString(params);
    const { sql, param } = ctx.helper.formatSql(createForm, formatParams);
    ctx.logger.info('createForm:', sql, param);
    await app.mysql.query(sql, param);
    return {
      schemaCode: params.schemaCode,
      schemaName: params.schemaName,
      schemaIcon: params.formProperty.schemaIcon.defaultValue,
    };
  }

  async renameForm(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(renameForm, params);
    ctx.logger.info('renameForm:', sql, params);
    await app.mysql.query(sql, param);
    return null;
  }

  async removeForm(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(removeForm, params);
    ctx.logger.info('removeForm:', sql, params);
    await app.mysql.query(sql, param);
    return null;
  }

  async saveForm(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const updateTime = new Date();
    Object.assign(params, { updateTime });
    const formatParams = ctx.helper.toJsonString(params);
    const { sql, param } = ctx.helper.formatSql(saveForm, formatParams);
    ctx.logger.info('saveForm:', sql, params);
    const res = await app.mysql.query(sql, param);
    return res;
  }

  async getFormData(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getFormData, params);
    ctx.logger.info('getFormData:', sql, params);
    // 查询表单
    const res = await app.mysql.query(sql, param);
    const form = ctx.helper.toJsonObj(res[0]);
    return form;
  }

  async createFormBiz(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const userid = await ctx.helper.getUserid(ctx);
    const userInfo = await ctx.service.user.getUserById({ userid });
    const data = {
      Creater: userInfo.username,
    };
    const filterParams = SetSystemControlValue(
      params,
      ['Creater', 'CreateTime'],
      data,
    );
    const formatParams = ctx.helper.toJsonString(filterParams);
    const { sql, param } = ctx.helper.formatSql(createFormBiz, formatParams);
    ctx.logger.info('createFormBiz:', sql, param);
    await app.mysql.query(sql, param);
    return null;
  }

  async getFormBiz(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(getFormBiz, params);
    ctx.logger.info('getFormBiz:', sql, params);
    let res: any = await app.mysql.query(sql, param);
    res = res.map((it: any) => {
      const item = ctx.helper.toJsonObj(it) || {};
      return item;
    });
    return res;
  }

  async removeFormBiz(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const { sql, param } = ctx.helper.formatSql(removeFormBiz, params);
    ctx.logger.info('removeFormBiz:', sql, params);
    await app.mysql.query(sql, param);
    return null;
  }

  async saveFormBiz(params: { [key: string]: any }) {
    const { ctx, app } = this;
    const updateTime = new Date();
    Object.assign(params, { updateTime });
    const filterParams = SetSystemControlValue(params, ['UpdateTime']);
    const formatParams = ctx.helper.toJsonString(filterParams);
    const { sql, param } = ctx.helper.formatSql(saveFormBiz, formatParams);
    ctx.logger.info('saveFormBiz:', sql, params);
    const res = await app.mysql.query(sql, param);
    return res;
  }
}
